<?php


namespace app\cms\model\saas;


use app\cms\community\lib\QueryModel;
use app\cms\dto\saas\saasClientServerDbInfo;
use iflow\Response;

class saasClientServerDbModel extends QueryModel
{
    const isSaasModel = true;
    protected $table = 'saas_client_server_db_table';

    public function getInfoById(int $server_id): array
    {
        $server = $this->where([
            'server_db_id' => $server_id
        ]) -> findOrEmpty();
        return $server -> isEmpty() ? [] : $server -> toArray();
    }

    /**
     * 获取saasDb服务器列表
     * @param string $server_db_name
     * @param string $server_db_ip
     * @param string $server_db_type
     * @param int $status
     * @return Response
     */
    public function getSaasClientDbServer(
        string $server_db_name = '',
        string $server_db_ip = '',
        string $server_db_type = 'mysql',
        int $status = -1
    ): Response
    {
        $where = [ [ 'server_db_type', '=', $server_db_type ] ];
        if ($server_db_ip !== '') {
            $where[] = [ 'server_db_ip', 'like', "%{$server_db_ip}%" ];
        }
        if ($server_db_name) {
            $where[] = [ 'server_db_name', 'like', "%{$server_db_name}%" ];
        }
        if ($status > -1) {
            $where[] = [ 'status', '=', $status ];
        }

        return $this->paginateQuery(
            $this->where($where)
        );
    }

    /**
     * 更新saas服务数据库信息
     * @param saasClientServerDbInfo $saasClientServerDbInfo
     * @return Response
     */
    public function updateSaasClientDbServer(
        saasClientServerDbInfo $saasClientServerDbInfo
    ): Response {
        return $this->updateInfo([
            'server_db_ip' => $saasClientServerDbInfo -> server_db_ip,
            'server_db_name' => $saasClientServerDbInfo -> server_db_name,
            'server_db_user' => $saasClientServerDbInfo -> server_db_user,
            'server_db_port' => $saasClientServerDbInfo -> server_db_port,
            'server_db_password' => $saasClientServerDbInfo -> server_db_password,
            'server_db_type' => $saasClientServerDbInfo -> server_db_type
        ], [
            'server_db_id' => $saasClientServerDbInfo -> server_db_id
        ]);
    }
}